<?php

class kelola_review {

//connect ke database SmartCommerce


	function __construct(){
			
		$db_config = new db_config();
		
		$link = mysql_connect($db_config->host.':'.$db_config->port, $db_config->db_username, $db_config->db_password);
		
		if (!$link) {
			die('Could not connect: '.mysql_error());
		}
		
		if(!mysql_select_db($db_config->db_name)) {
			die("cannot select db");
		}
		
	}
	
	function select_review($id_techno){
	
		$sql = '
			SELECT 
				rp.ID_REVIEW_PRODUK, rp.REVIEW_PRODUK, rp.WAKTU, rp.EMAIL_GUEST, rp.CONFIRMED, p.NAMA_PRODUK, p.ID_PRODUK 
			FROM 
				review_produk as rp 
			LEFT JOIN 
				produk as p ON rp.ID_PRODUK = p.ID_PRODUK 
			LEFT JOIN 
				account_technopreneur as at on p.ID_TECHNOPRENEUR = at.ID_TECHNOPRENEUR
			WHERE 
				at.ID_TECHNOPRENEUR = '.$id_techno.'
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function select_review_confirmed($id_produk){
	
		$sql = '
			SELECT 
				* 
			FROM 
				review_produk 
			WHERE 
				ID_PRODUK = '.$id_produk.' 
				AND
				CONFIRMED = 1 
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function select_review_unconfirmed($id_produk){
	
		$sql = '
			SELECT 
				* 
			FROM 
				review_produk 
			WHERE 
				ID_PRODUK = '.$id_produk.' 
				AND
				CONFIRMED = 0 
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function insert_review($data){
		
		extract($data, EXTR_OVERWRITE);
		
		$sql = '
			INSERT INTO 
				review_produk 
				(ID_PRODUK, REVIEW_PRODUK, WAKTU, EMAIL_GUEST, CONFIRMED) 
			VALUES
				('.$ID_PRODUK.',"'.$REVIEW_PRODUK.'","'.$WAKTU.'","'.$EMAIL_GUEST.'",'.$CONFIRMED.')
		';
		
		mysql_query($sql);
		
		$id_review = mysql_insert_id();
		
		return $id_review;
		
	}
	
	function confirm_review($id_review){
		
		$sql = '
			UPDATE 
				review_produk 
			SET 
				CONFIRMED = 1 
			WHERE 
				ID_REVIEW_PRODUK = '.$id_review.'
		';
		
		mysql_query($sql);
	}
	
	function unconfirm_review($id_review){
		
		$sql = '
			UPDATE 
				review_produk 
			SET 
				CONFIRMED = 0 
			WHERE 
				ID_REVIEW_PRODUK = '.$id_review.'
		';
		
		mysql_query($sql);
	}
	
	function delete_all_review($id_produk){
	
		$sql = '
			DELETE FROM
				review_produk 
			WHERE
				ID_PRODUK = '.$id_produk.'
		';
		
		mysql_query($sql);
		
	}
	
}

?>